﻿Imports Licenta3.Common.Framework
Imports Licenta3.Commanding
Imports Microsoft.Office.Interop.Word
Imports System.IO
Imports System.Data
Imports Licenta3.Common.Classes
Imports System.Collections.ObjectModel

Public MustInherit Class AnexaViewModel(Of AnexaT As {AnexaClass, New})
    Inherits ObjectBase

    Private _Anexa As New AnexaT
    Public Overridable Property Anexa As AnexaT
        Get
            Return _Anexa
        End Get
        Set(value As AnexaT)
            _Anexa = value
            OnPropertyChanged("Anexa")
        End Set
    End Property
    Private _AnexaXMLSource As String
    Private _UATImobilList As New ObservableCollection(Of UAT)
    Public Property UATImobilList As ObservableCollection(Of UAT)
        Get
            Return _UATImobilList
        End Get
        Private Set(value As ObservableCollection(Of UAT))
            _UATImobilList = value
            OnPropertyChanged("UATImobilList")
        End Set
    End Property
    Public Property AnexaXMLSource As String
        Get
            Return _AnexaXMLSource
        End Get
        Set(value As String)
            _AnexaXMLSource = value
            OnPropertyChanged("AnexaXMLSource")
        End Set
    End Property
    Public Property SelectFileDialogCommand As ICommand
    Private _UpdateUATImobilListCommand As ICommand
    Public Property UpdateUATImobilListCommand As ICommand
        Get
            Return _UpdateUATImobilListCommand
        End Get
        Set(value As ICommand)
            _UpdateUATImobilListCommand = value
            OnPropertyChanged("UpdateUATImobilListCommand")
        End Set
    End Property
    Private _GenerateAnexaOutCommand As ICommand
    Public Property GenerateAnexaOutCommand As ICommand
        Get
            Return _GenerateAnexaOutCommand
        End Get
        Set(value As ICommand)
            _GenerateAnexaOutCommand = value
            OnPropertyChanged("GenerateAnexaOutCommand")
            'RaiseEvent 
        End Set
    End Property
    Public Property ToXMLCommand As ICommand
    Public Property ResetModelCommand As ICommand
    Private _Setari As New SetariClass
    Public ReadOnly Property Setari As SetariClass
        Get
            Return _Setari
        End Get
    End Property
    Public Sub New()
        _SelectFileDialogCommand = New DelegateCommand(Of String)(AddressOf SelectFileDialog, AddressOf CanSelectFileDialog)
        _UpdateUATImobilListCommand = New DelegateCommand(Of Object)(AddressOf UpdateAnexaUAT, AddressOf CanUpdateAnexaUAT)
        _GenerateAnexaOutCommand = New DelegateCommand(Of String)(AddressOf GenerateAnexaOut, AddressOf CanGenerateAnexaOut)
        _ToXMLCommand = New DelegateCommand(Of Object)(AddressOf ToXML, AddressOf canToXML)
        _ResetModelCommand = New DelegateCommand(Of Object)(AddressOf ResetModel, AddressOf CanResetModel)
        Me.UpdateAnexaUAT()
        If Anexa.Imobil.UAT.Denumire Is Nothing Then
            'Anexa.Imobil.UAT = New UAT
            Anexa.Imobil.UAT = UATImobilList.FirstOrDefault
        End If

    End Sub

    Public Sub UpdateAnexaUAT()
        Dim AUDS As New DataSet("Administrativeunit")
        If IO.File.Exists(assemblyPath + "\Administrativeunit.xml") Then
            AUDS = DS.Administrativeunit
            Dim selection As DataRow()
            Dim __UATImobilList As New ObservableCollection(Of UAT)
            Dim row As DataRow

            If Not IsNothing(AUDS) Then
                If Not IsNothing(Anexa) Then
                    selection = AUDS.Tables("Administrativeunit").Select("COUNTYID=" & Anexa.OCPI.cod, "NAME")
                Else
                    selection = AUDS.Tables("Administrativeunit").Select("COUNTYID=" & Setari.currentAutorizat.DefaultOCPI.cod, "NAME")
                End If
                For i = 0 To selection.Count - 1
                    row = selection.ElementAt(i)
                    Dim UAT As New UAT
                    UAT.Denumire = row.Item("NAME").ToString
                    UAT.SIRSUP = row.Item("SIRUTA").ToString
                    UAT.CountyId = CInt(row.Item("COUNTYID"))
                    UAT.AdministrativeUnitId = CInt(row.Item("ADMINISTRATIVEUNITID"))
                    __UATImobilList.Add(UAT)

                Next
                UATImobilList = __UATImobilList
            End If
            End If
    End Sub
    Public Function CanUpdateAnexaUAT()
        Return True
    End Function
    Public Sub SelectFileDialog()
        'Dim dialog As New System.Windows.Forms.FolderBrowserDialog
        Dim OpenFileDialog As New Microsoft.Win32.OpenFileDialog
        OpenFileDialog.InitialDirectory = SetariClass._LucruPath
        OpenFileDialog.ShowDialog()
        Me.AnexaXMLSource = OpenFileDialog.FileName
        If File.Exists(OpenFileDialog.FileName) Then
            Me.Anexa.ReadFromXML(OpenFileDialog.FileName)
        End If

    End Sub
    Public Function CanSelectFileDialog()
        Return True
    End Function

    Public Sub GenerateAnexaOut(outFormat As String)
        'MsgBox(Anexa.proprietar.Nume)
        Select Case True
            Case outFormat.ToLower.Contains("word".ToLower)
                Anexa.toWordOrPDF()
            Case outFormat.ToLower.Contains("pdf".ToLower)
                Anexa.toWordOrPDF(WdSaveFormat.wdFormatPDF)
            Case outFormat.ToLower.Contains("print".ToLower) Or outFormat.ToLower.Contains("imprim".ToLower)
                Anexa.toWordOrPDF(, True)
        End Select
    End Sub
    Public Function CanGenerateAnexaOut()
        Return True
    End Function

    Public Sub ToXML()
        'MsgBox(Anexa.proprietar.Nume)
        Dim currentDirectory = SetariClass._LucruPath & "\" & Anexa.Autorizat.Nume & "\" & Anexa.OCPI.denumire & "\" & Anexa.Imobil.UAT.Denumire & "\" & Anexa.proprietar.Nume
        If IO.Directory.Exists(currentDirectory) = False Then
            IO.Directory.CreateDirectory(currentDirectory)
        End If
        Anexa.SaveToXML(currentDirectory & "\" & Anexa.ShortName & ".xml")
    End Sub
    Public Function canToXML() As Boolean
        Return True
    End Function
    Public Sub ResetModel()
        'Me.Anexa = Nothing
        Me.Anexa = New AnexaT
    End Sub
    Public Function CanResetModel() As Boolean
        Return True
    End Function
End Class
